import Vue from 'vue'
import Router from 'vue-router'
import login from '@/page/login'
// import leftMenu from '@/page/common/leftMenu'
// import head from '@/page/common/head'
// import main from '@/page/common/main'
import home from '@/page/home'

//消息配置
import configManage from '@/page/message/configManage'
import sendMessage from '@/page/message/sendMessage'





Vue.use(Router)

const router = new Router({
  //去除前端url中的#
  // mode:'history',
  //前端统一的url
  base: 'message-push',
  routes: [
    //访问/ 重定向到login页面
    { path: '/', redirect: '/login' },
    { path: '/login', component: login },
    {
      path: "/home",
      name: "home",
      component: home,
      children: [
        {
          path: '/configManage',
          name: 'configManage',
          meta: {
            title: '消息配置'
          },
          component: configManage
        },
        {
          path: '/sendMessage',
          name: 'sendMessage',
          meta: {
            title: '发送消息'
          },
          component: sendMessage
        }]
    },   
  ]
});
//挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.localStorage.getItem('Authorization')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})

// route.beforeEach((to, from, next) => {
//   if (to.path === '/') {
//     next({ path: '/login' })
//     return
//   }
//   if (to.path === '/login') {
//     next()
//   } 
// })
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
export default router;